<?php

/**
 * This is the model class for table "{{object_price}}".
 *
 * The followings are the available columns in table '{{object_price}}':
 * @property string $price_id
 * @property string $price_key
 * @property string $price_value
 * @property string $object_id
 */
class ObjectPrice extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return ObjectPrice the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{object_price}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {

        return array(
            array('price_key, price_value, object_id', 'required'),
            array('price_key', 'length', 'max' => 10),
            array('price_value, object_id', 'length', 'max' => 20),
            array('price_id, price_key, price_value, object_id', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {

        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'price_id' => 'Price',
            'price_key' => 'Price Key',
            'price_value' => 'Price Value',
            'object_id' => 'Object',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {

        $criteria = new CDbCriteria;

        $criteria->compare('price_id', $this->price_id, true);
        $criteria->compare('price_key', $this->price_key, true);
        $criteria->compare('price_value', $this->price_value, true);
        $criteria->compare('object_id', $this->object_id, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public static function getPrice($objID) {
        $currency = Yii::app()->user->getState('currency');
        $price = ObjectPrice::model()->findByAttributes(array('object_id' => $objID, 'price_key' => 'price_' . $currency));
        if (isset($price)) {         
            return $price->price_value;
        } else {         
            $price = ObjectPrice::model()->findByAttributes(array('object_id' => $objID, 'price_key' => 'price_USD'));         
            $priceTrue = Currency::convert('USD', $currency, $price->price_value);         
            return $priceTrue;
        }
    }

    public static function GetObjectPrice($object_id) {
        $currency = Yii::app()->user->getState('currency');
        $criteria = new CDbCriteria;
        $criteria->condition = 'object_id=:oid && currency=:cur';
        $criteria->params = array(':oid' => $object_id, ':cur' => $currency);
        $ObjectPrice = ObjectPrice::model()->find($criteria);
        return $ObjectPrice;
    }

    public static function FindAllListPriceObject($object_id) {
        $criteria = new CDbCriteria;
        $criteria->condition = 'object_id=:oid';
        $criteria->params = array(':oid' => $object_id);
        $criteria->order = 't.price_id ASC';
        $ObjectPrice = ObjectPrice::model()->findAll($criteria);
        return $ObjectPrice;
    }

}
